﻿using OfficeOpenXml;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FastData
{
   public class ExcelHelper
    {
        public static DataSet WorksheetToDataSet(string path)
        {
            DataSet dataSet = new DataSet();
            FileInfo file = new FileInfo(path);//数据表
            var package = new ExcelPackage(file);
            ExcelWorkbook workbook = package.Workbook;
            ExcelWorksheets excelWorksheets = workbook.Worksheets;
            foreach (ExcelWorksheet worksheet in excelWorksheets)
            {
                object[,] aa = (object[,])worksheet.Cells.Value;
                if (aa != null)
                {
                    DataTable dt = new DataTable();
                    for (int k = 0; k < aa.GetLength(1); k++)
                    {
                        dt.Columns.Add(aa[0, k].ToString());
                    }

                    for (int i = 1; i < aa.GetLength(0); i++)
                    {
                        object[] obj = new object[aa.GetLength(1)];
                        for (int j = 0; j < aa.GetLength(1); j++)
                        {
                            obj[j] = aa[i, j];
                        }
                        dt.Rows.Add(obj);
                    }
                    dataSet.Tables.Add(dt);
                }
            }
            return dataSet;
        }

        public ExcelWorksheets OpenWorkSheet(string path)
        {
            FileInfo file = new FileInfo(path);//数据表
            DataTable dt = null;
            var package = new ExcelPackage(file);
            ExcelWorkbook workbook = package.Workbook;
            return workbook.Worksheets;
        }
    }
}
